#include <QDebug>

#include "Async.h"
#include "FileIO.h"

Async::Async(QObject *parent):
    QThread(parent)
{
}

Async::~Async()
{
}

void Async::stop()
{
    QThread::terminate();
}

void Async::md5(const QString &fileName)
{
    command = "md5";
    params.clear();
    params << fileName;
    start();
}

void Async::md5List(const QStringList &fileNames)
{
    command = "md5List";
    params = fileNames;
    start();
}

void Async::run()
{
    qDebug() << "Async: " << command;// << ": " << params;

    if (command == "md5") {
        const QString &fileName = params.first();
        QString resultValue = FileIO().md5(fileName);
        result(QStringList() << fileName << resultValue);
    } else if (command == "md5List") {
        FileIO fileIO;
        foreach (const QString &fileName, params) {
            qDebug() << "Checking " << fileName;
            QString resultValue = fileIO.md5(fileName);
            result(QStringList() << fileName << resultValue);
        }
    } else {
        qDebug() << "Unexpected command: " << command;
    }

    finished();
}
